package org.bo.actions.modulefunction;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bo.LogInformation;
import org.bo.entity.ModuleFunction;
import org.bo.persistence.hibernate.HibernateSessionFactory;
import org.bo.persistence.hibernate.HibernateSessionFactoryAware;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;

public class SearchModuleFunction extends ModuleFunctionForm implements
		HibernateSessionFactoryAware {
	private HibernateSessionFactory hsf;
	private Session sess;
	private List moduleFunctions = new ArrayList();
	private int maxPage, currPage, nextPage, prevPage = 0, page = 0;
	private int maxRowPerPage = 10;
	private String orderBy = "name";
	private int resultRows;

	public String execute() {
		try {
			sess = hsf.createSession();
			Criteria crit = sess.createCriteria(ModuleFunction.class);

			if (!getName().equalsIgnoreCase("")) {
				crit.add(Expression.like("name", "%" + getName() + "%"));
			}
			if (!getDescription().equalsIgnoreCase("")) {
				crit.add(Expression.like("description", "%" + getDescription()
						+ "%"));
			}
			if (!getModuleFunctionId().equalsIgnoreCase("")) {
				crit.add(Expression.like("moduleFunction.Id", "%"
						+ getModuleFunctionId() + "%"));
			}
			if (!getModuleDescriptorId().equalsIgnoreCase("")) {
				crit.add(Expression.like("moduleDescriptors.Id", "%"
						+ getModuleDescriptorId() + "%"));
			}
			crit.add(Expression.eq("logInformation.activeFlag",
					LogInformation.ACTIVE));
			crit.add(Expression.ne("id", "0")); // Expression Not Equals
			resultRows = crit.list().size();

			maxPage = resultRows / maxRowPerPage;
			if (resultRows % maxRowPerPage == 0)
				maxPage = maxPage - 1;

			moduleFunctions = crit.addOrder(Order.asc(orderBy)).setFirstResult(
					currPage * maxRowPerPage).setMaxResults(maxRowPerPage)
					.list();

			prevPage = currPage - 1;
			nextPage = currPage + 1;
			page = currPage + 1;

			hsf.endSession(sess);
			// hsf.closeSession(sess);
			return SUCCESS;
		} catch (HibernateException e) {
			return ERROR;
		} catch (SQLException e) {
			return ERROR;
		}
	}

	public void setHibernateSessionFactory(HibernateSessionFactory hsf) {
		this.hsf = hsf;

	}

	/**
	 * @return Returns the moduleFunctions.
	 */
	public List getModuleFunctions() {
		return moduleFunctions;
	}

	/**
	 * @param moduleFunctions
	 *            The moduleFunctions to set.
	 */
	public void setModuleFunctions(List moduleFunctions) {
		this.moduleFunctions = moduleFunctions;
	}

	/**
	 * @return Returns the currPage.
	 */
	public int getCurrPage() {
		return currPage;
	}

	/**
	 * @param currPage
	 *            The currPage to set.
	 */
	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	/**
	 * @return Returns the maxPage.
	 */
	public int getMaxPage() {
		return maxPage;
	}

	/**
	 * @param maxPage
	 *            The maxPage to set.
	 */
	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	/**
	 * @return Returns the maxRowPerPage.
	 */
	public int getMaxRowPerPage() {
		return maxRowPerPage;
	}

	/**
	 * @param maxRowPerPage
	 *            The maxRowPerPage to set.
	 */
	public void setMaxRowPerPage(int maxRowPerPage) {
		this.maxRowPerPage = maxRowPerPage;
	}

	/**
	 * @return Returns the nextPage.
	 */
	public int getNextPage() {
		return nextPage;
	}

	/**
	 * @param nextPage
	 *            The nextPage to set.
	 */
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	/**
	 * @return Returns the orderBy.
	 */
	public String getOrderBy() {
		return orderBy;
	}

	/**
	 * @param orderBy
	 *            The orderBy to set.
	 */
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	/**
	 * @return Returns the prevPage.
	 */
	public int getPrevPage() {
		return prevPage;
	}

	/**
	 * @param prevPage
	 *            The prevPage to set.
	 */
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	/**
	 * @return Returns the resultRows.
	 */
	public int getResultRows() {
		return resultRows;
	}

	/**
	 * @param resultRows
	 *            The resultRows to set.
	 */
	public void setResultRows(int resultRows) {
		this.resultRows = resultRows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
